package com.git.hui.yinshua.biz.rest.app.user;

import com.git.hui.yinshua.api.model.ReqInfoContext;
import com.git.hui.yinshua.api.model.ResVo;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.permission.Permission;
import com.git.hui.yinshua.api.model.permission.UserAccessRole;
import com.git.hui.yinshua.biz.rest.models.user.res.UserAddressDto;
import com.git.hui.yinshua.biz.user.service.UserAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户地址
 *
 * @author YiHui
 * @date 2024/6/11
 */
@RequestMapping(path = "app/address")
@RestController
@Permission(role = UserAccessRole.LOGIN)
public class AddressController {
    @Autowired
    private UserAddressService userService;

    @RequestMapping(path = "add")
    public ResVo<Boolean> addAddress(@RequestBody UserAddressDto address) {
        // 参数校验
        if (StringUtils.isBlank(address.getAddress())) {
            // 参数异常
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "详细地址不能为空");
        }
        if (StringUtils.isBlank(address.getArea())) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "地址不能为空");
        }
        if (StringUtils.isBlank(address.getPerson())) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "收货人不能为空");
        }
        if (StringUtils.isBlank(address.getPhone())) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "手机号非法");
        }

        return ResVo.ok(userService.addAddress(ReqInfoContext.getReqInfo().getUserId(), address));
    }

    /**
     * 更新地址
     *
     * @param address
     * @return
     */
    @RequestMapping(path = "update")
    public ResVo<Boolean> update(@RequestBody UserAddressDto address) {
        // 参数校验
        if (StringUtils.isBlank(address.getAddress())) {
            // 参数异常
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "详细地址不能为空");
        }
        if (StringUtils.isBlank(address.getArea())) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "地址不能为空");
        }
        if (StringUtils.isBlank(address.getPerson())) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "收货人不能为空");
        }
        if (StringUtils.isBlank(address.getPhone())) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "手机号非法");
        }
        if (address.getAddressId() == null) {
            return ResVo.error(Status.StatusEnum.ILLEGAL_ARGUMENTS_MIXED, "缺少地址信息");
        }

        userService.updateAddress(ReqInfoContext.getReqInfo().getUserId(), address);
        return ResVo.ok(true);
    }

    @RequestMapping(path = "delete")
    public ResVo<Boolean> deleteAddress(Integer addressId) {
        return ResVo.ok(userService.deleteAddress(ReqInfoContext.getReqInfo().getUserId(), addressId));
    }

    @RequestMapping(path = "markDefault")
    public ResVo<Boolean> markAddressDefault(Integer addressId, Boolean main) {
        userService.updateMainAddress(ReqInfoContext.getReqInfo().getUserId(), addressId, main);
        return ResVo.ok(true);
    }

    @RequestMapping(path = "list")
    public ResVo<List<UserAddressDto>> list() {
        List<UserAddressDto> list = userService.listUserAddress(ReqInfoContext.getReqInfo().getUserId());
        return ResVo.ok(list);
    }

    @GetMapping(path = "main")
    public ResVo<UserAddressDto> mainAddress() {
        UserAddressDto addressDto = userService.getDefaultUserAddress(ReqInfoContext.getReqInfo().getUserId());
        return ResVo.ok(addressDto);
    }
}
